// src/tests/mock-api-test.js
// Mock API 测试文件，用于验证mock数据的完整性和功能

import { mockAPI } from '@/api/mock.js';

// 测试函数
async function testMockAPI() {
  console.log('开始测试 Mock API...');
  
  try {
    // 测试党支部列表API
    console.log('\n1. 测试党支部列表API...');
    const branchList = await mockAPI.branch.list({ page: 1, pageSize: 5 });
    console.log('✅ 党支部列表获取成功:', branchList.list.length, '个党支部');
    console.log('   第一个党支部:', branchList.list[0]?.branchName);
    
    // 测试党支部详情API
    if (branchList.list.length > 0) {
      console.log('\n2. 测试党支部详情API...');
      const branchId = branchList.list[0].branchId;
      const branchDetail = await mockAPI.branch.detail({ branchId });
      console.log('✅ 党支部详情获取成功:', branchDetail.branchName);
      console.log('   支委数量:', branchDetail.branchCommittees?.length || 0);
      console.log('   活动数量:', branchDetail.branchActivities?.length || 0);
      console.log('   成员数量:', branchDetail.currentMembers?.length || 0);
    }
    
    // 测试党员列表API
    console.log('\n3. 测试党员列表API...');
    const memberList = await mockAPI.member.list({ page: 1, pageSize: 5 });
    console.log('✅ 党员列表获取成功:', memberList.list.length, '个党员');
    console.log('   第一个党员:', memberList.list[0]?.memberName);
    
    // 测试党员详情API
    if (memberList.list.length > 0) {
      console.log('\n4. 测试党员详情API...');
      const memberId = memberList.list[0].memberId;
      const memberDetail = await mockAPI.member.detail({ memberId });
      console.log('✅ 党员详情获取成功:', memberDetail.memberName);
      console.log('   政治面貌:', memberDetail.politicalStatus);
      console.log('   所属党支部:', memberDetail.currentPartyBranch);
    }
    
    // 测试搜索功能
    console.log('\n5. 测试搜索功能...');
    const searchResult = await mockAPI.branch.list({ search: '党支部', page: 1, pageSize: 10 });
    console.log('✅ 搜索功能正常，找到', searchResult.list.length, '个结果');
    
    // 测试筛选功能
    console.log('\n6. 测试筛选功能...');
    const filterResult = await mockAPI.branch.list({ branchType: '本科', page: 1, pageSize: 10 });
    console.log('✅ 筛选功能正常，找到', filterResult.list.length, '个本科党支部');
    
    // 测试导出功能
    if (branchList.list.length > 0) {
      console.log('\n7. 测试导出功能...');
      const branchId = branchList.list[0].branchId;
      const exportResult = await mockAPI.branch.exportData({ branchId });
      console.log('✅ 导出功能正常，文件名:', exportResult.fileName);
    }
    
    // 测试筛选选项API
    console.log('\n8. 测试筛选选项API...');
    const filterOptions = await mockAPI.member.filterOptions();
    console.log('✅ 筛选选项获取成功');
    console.log('   政治面貌选项:', filterOptions.politicalStatus?.length || 0, '个');
    console.log('   专业选项:', filterOptions.majors?.length || 0, '个');
    console.log('   班级选项:', filterOptions.classes?.length || 0, '个');
    
    console.log('\n🎉 所有Mock API测试通过！');
    
  } catch (error) {
    console.error('\n❌ Mock API测试失败:', error);
  }
}

// 数据完整性检查
function checkDataIntegrity() {
  console.log('\n开始检查数据完整性...');
  
  // 检查党支部数据结构
  console.log('\n1. 检查党支部数据结构...');
  mockAPI.branch.list({ page: 1, pageSize: 1 }).then(result => {
    if (result.list.length > 0) {
      const branch = result.list[0];
      const requiredFields = ['branchId', 'branchName', 'branchType', 'branchDescription'];
      const missingFields = requiredFields.filter(field => !branch[field]);
      
      if (missingFields.length === 0) {
        console.log('✅ 党支部数据结构完整');
      } else {
        console.log('❌ 党支部数据缺少字段:', missingFields);
      }
    }
  });
  
  // 检查党员数据结构
  console.log('\n2. 检查党员数据结构...');
  mockAPI.member.list({ page: 1, pageSize: 1 }).then(result => {
    if (result.list.length > 0) {
      const member = result.list[0];
      const requiredFields = ['memberId', 'memberName', 'politicalStatus', 'currentPartyBranch'];
      const missingFields = requiredFields.filter(field => !member[field]);
      
      if (missingFields.length === 0) {
        console.log('✅ 党员数据结构完整');
      } else {
        console.log('❌ 党员数据缺少字段:', missingFields);
      }
    }
  });
}

// 导出测试函数
export { testMockAPI, checkDataIntegrity };

// 如果直接运行此文件，执行测试
if (typeof window !== 'undefined') {
  // 浏览器环境
  window.testMockAPI = testMockAPI;
  window.checkDataIntegrity = checkDataIntegrity;
  console.log('Mock API 测试函数已加载到 window 对象');
  console.log('在浏览器控制台中运行 testMockAPI() 来执行测试');
}